<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2020/10/12
 * Time: 4:06 PM
 */
namespace app\admin\logic;


use app\model\Supplies;
use app\model\SuppliesType;
use app\model\Warehouse;
use think\facade\Db;
use think\facade\Request;

class SuppliesLogic
{
    public function getType()
    {
        $typeModel = new SuppliesType();
        return $typeModel->getSuppliesTypeList();
    }

    public function getList()
    {
        $param = Request::get();

        $where = [
            ['s_status','=',1],
            ['s_orgid','=',config('user')['orgid']]
        ];
        if(isset($param['type']) || !empty($param['type'])){
            $where[] = ['s_type','=',intval($param['type'])];
        }

        $suppliesModel = new Supplies();
        $list = $suppliesModel->getSuppliesListByCondition($where);
        return $list;
    }

    public function add()
    {
        $param = Request::post();

        $data = [
            's_orgid' => config('user.orgid'),
            's_uid' => config('user.id'),
        ];
        if(!isset($param['name']) || empty($param['name'])) fail('请输入商品名称');
        $data['s_name'] = trim($param['name']);

        if(!isset($param['type']) || empty($param['type'])) fail('请选择类型');
        $data['s_type'] = intval($param['type']);

        if(!isset($param['brand']) || empty($param['brand'])) fail('请输入品牌名称');
        $data['s_brand'] = trim($param['brand']);

        if(!isset($param['spec']) || empty($param['spec'])) fail('请输入规格');
        $data['s_spec'] = trim($param['spec']);

        if(!isset($param['unit']) || empty($param['unit'])) fail('请输入单位');
        $data['s_unit'] = trim($param['unit']);

        $suppliesModel = new Supplies();
        Db::startTrans();
        //添加
        $suppliesId = $suppliesModel->addOneSupplies($data);
        if(!$suppliesId){
            Db::rollback();
            fail('商品添加失败');
        }
        //进入仓库
        $warehouseModel = new Warehouse();
        $warehouse = [
            'w_supplies_id' => $suppliesId,
            'w_orgid' => config('user.orgid'),
            'w_uid' => config('user.id')
        ];
        $warehouseId = $warehouseModel->addOneWarehouse($warehouse);
        if(!$warehouseId){
            Db::rollback();
            fail('商品添加仓库失败');
        }
        Db::commit();
        return true;
    }

    public function update()
    {
        $param = Request::post();

        $data = [
            's_uid' => config('user.id'),
            's_update_time' => time(),
        ];

        if(!isset($param['suppliesId']) || empty($param['suppliesId'])) fail('请选择商品');
        $suppliesId = intval($param['suppliesId']);
        $suppliesModel = new Supplies();
        $suppliesInfo = $suppliesModel->getSuppliesInfoById($suppliesId);
        if(!$suppliesInfo)  fail('商品不存在');

        if(!isset($param['name']) || empty($param['name'])) fail('请输入商品名称');
        $data['s_name'] = trim($param['name']);

        if(!isset($param['type']) || empty($param['type'])) fail('请选择类型');
        $data['s_type'] = intval($param['type']);

        if(!isset($param['brand']) || empty($param['brand'])) fail('请输入品牌名称');
        $data['s_brand'] = trim($param['brand']);

        if(!isset($param['spec']) || empty($param['spec'])) fail('请输入规格');
        $data['s_spec'] = trim($param['spec']);

        if(!isset($param['unit']) || empty($param['unit'])) fail('请输入单位');
        $data['s_unit'] = trim($param['unit']);

        $rt = $suppliesModel->where(['s_id'=>$suppliesId])->update($data);
        if(!$rt)    fail('商品修改失败');
        return true;

    }

    public function del()
    {
        $param = Request::delete();

        $data = [
            's_uid' => config('user.id'),
            's_update_time' => time(),
            's_status' => 0, //删除
        ];

        if(!isset($param['suppliesId']) || empty($param['suppliesId'])) fail('请选择商品');
        $suppliesId = intval($param['suppliesId']);
        $suppliesModel = new Supplies();
        $suppliesInfo = $suppliesModel->getSuppliesInfoById($suppliesId);
        if(!$suppliesInfo)  fail('商品不存在');
        if($suppliesInfo['status'] == 0)  fail('商品已删除');


        $rt = $suppliesModel->where(['s_id'=>$suppliesId])->update($data);
        if(!$rt)    fail('商品删除失败');
        return true;

    }
}